﻿using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_146 : BaseProblem
    {
        public override object GetResult()
        {
            const int max = 150000000;

            var lst = new List<BigInteger> {1, 3, 7, 9, 13, 27};

            BigInteger sm = 0;

            for (long p = 1; p <= max/10; p++)
            {
                if (p %3 == 0 || p%7 == 0 || p%13 ==0)
                    continue;
                BigInteger n = 100*p*p;
                var ex = false;
                for (BigInteger l = lst[0]; l <= lst[lst.Count - 1]; l+=2 )
                {
                    if (lst.Contains(l))
                    {
                        if ((n+l).RabinMillerTest(7))
                            continue;
                        ex = true;
                        break;
                    }
                    if (!(n+l).RabinMillerTest(7))
                        continue;
                    ex = true;
                    break;
                }

                if (ex)
                    continue;
                sm += p;
            }
            return 10*sm;
        }

        public override string Problem
        {
            get
            {
                return @"The smallest positive integer n for which the numbers n2+1, n2+3, n2+7, n2+9, n2+13, and n2+27 are consecutive primes is 10. The sum of all such integers n below one-million is 1242490.

What is the sum of all such integers n below 150 million?";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 676333270;
            }
        }

    }
}
